import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: PointDemo(),
    );
  }
}

class PointDemo extends StatefulWidget {
  @override
  _PointDemoState createState() => _PointDemoState();
}

class _PointDemoState extends State<PointDemo> {
  Offset? _clickOffset;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('绘制专题')),
      body: Center(
        child: GestureDetector(
          onPanUpdate: (DragUpdateDetails details) {
            //更新手指的位置
            _clickOffset = details.localPosition;
            setState(() {});
          },
          child: Container(
            width: 200,
            height: 200,
            color: Colors.blue.shade100,
            child: CustomPaint(
              size: Size(150, 150),
              //画布
              //绘制工作
              painter: _PointCustomPainter(_clickOffset),
            ),
          ),
        ),
      ),
    );
  }
}

class _PointCustomPainter extends CustomPainter {
  Paint _paint = Paint()
    //画笔的颜色
    ..color = Colors.red
    //设置拐角的样式
    ..strokeCap = StrokeCap.round
    //设置宽度
    ..strokeWidth = 10;

  Offset? clickOffset;
  _PointCustomPainter(this.clickOffset);
  @override
  void paint(Canvas canvas, Size size) {
    //绘制点
    canvas.drawPoints(
      //绘制的模式   PointMode.points 点   PointMode.lines 线 两个点
      PointMode.points,
      //绘制的点
      [Offset(0, 0), Offset(100, 0)],
      //画笔
      _paint,
    );
    if (clickOffset != null) {
      _paint.color = Colors.blue;
      _paint.strokeWidth = 50;
      canvas.drawPoints(PointMode.points, [clickOffset!], _paint);

      _paint.color = Colors.red;
      _paint.strokeWidth = 10;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
